package org.jeecg.modules.demo.cashier;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.weixin.entity.SmtStoredValueUsers;
import org.jeecg.modules.demo.weixin.smtstoredvalueusers.service.ISmtStoredValueUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description: 用户储值卡查询
 * @Author: jeecg-boot
 * @Date: 2023-10-30
 * @Version: V1.0
 */
@Api(tags = "用户储值卡查询")
@RestController
@RequestMapping("/weixin/cashierstoredvalueusers/cashierStoredValueUsers")
@Slf4j
public class CashierStoredValueUsersController extends JeecgController<SmtStoredValueUsers, ISmtStoredValueUsersService> {
    @Autowired
    private ISmtStoredValueUsersService smtStoredValueUsersService;

    /**
     * 用户储值卡分页列表查询
     *
     * @param smtStoredValueUsers
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<IPage<SmtStoredValueUsers>> queryPageList(SmtStoredValueUsers smtStoredValueUsers,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            HttpServletRequest req) {
        QueryWrapper<SmtStoredValueUsers> queryWrapper = new QueryWrapper<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (smtStoredValueUsers.getUserId() != null) {
            queryWrapper.eq("user_id", smtStoredValueUsers.getUserId());
        }
        if (StringUtils.isNoneBlank(smtStoredValueUsers.getPhone())) {
            queryWrapper.eq("phone", smtStoredValueUsers.getPhone());
        }
        if (StringUtils.isNoneBlank(smtStoredValueUsers.getValueNumber())) {
            queryWrapper.eq("value_number", smtStoredValueUsers.getValueNumber());
        }
        Page<SmtStoredValueUsers> page = new Page<SmtStoredValueUsers>(pageNo, pageSize);
        IPage<SmtStoredValueUsers> pageList = smtStoredValueUsersService.page(page, queryWrapper);
        if (CollectionUtils.isEmpty(pageList.getRecords())) {
            return Result.ok(new Page<>());
        }
        List<SmtStoredValueUsers> counts = new ArrayList<>();
        Stream<SmtStoredValueUsers> storedValueUsersStream = pageList.getRecords().stream().filter(SmtOnceCardUser1 ->
                SmtOnceCardUser1.getValueValidDate().equals("无限制")
        );
        if (storedValueUsersStream!=null) {
            List<SmtStoredValueUsers> valueUsers =storedValueUsersStream.collect(Collectors.toList());
            counts.addAll(valueUsers);
        }
        pageList.getRecords().forEach(card -> {
            if (!card.getValueValidDate().equals("无限制")) {
                if (LocalDate.parse(card.getValueValidDate(), dateTimeFormatter).compareTo(LocalDate.now()) > 0) {
                    counts.add(card);
                }
                else {
                    //1 是过期了
                    card.setStatus(1);
                    //进行修改
                    smtStoredValueUsersService.updateById(card);
                }
            }

        });
        pageList.setRecords(counts);
        pageList.setTotal(counts.size());

        return Result.OK(pageList);
    }

    /**
     * 根据手机号码查询用户储值卡
     *
     * @param phone
     * @return
     */
    @GetMapping(value = "/queryByPhone")
    public Result<List<SmtStoredValueUsers>> queryById(@RequestParam String phone) {
        LambdaQueryWrapper<SmtStoredValueUsers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtStoredValueUsers::getPhone, phone);
        queryWrapper.orderByDesc(SmtStoredValueUsers::getCreateTime);
        List<SmtStoredValueUsers> smtStoredValueUsersList = smtStoredValueUsersService.list(queryWrapper);
        return Result.OK(smtStoredValueUsersList);
    }


    /**
     * 添加
     *
     * @param smtStoredValueUsers
     * @return
     */
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SmtStoredValueUsers smtStoredValueUsers) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        smtStoredValueUsers.setValueNumber(simpleDateFormat.format(new Date()) + RandomUtil.randomNumbers(7));
        smtStoredValueUsers.setMerchantId(Integer.valueOf(smtStoredValueUsers.getMerId()));
        smtStoredValueUsersService.save(smtStoredValueUsers);
        return Result.OK("添加成功！");
    }


}
